package com.seg.web.controller.wms;

import com.seg.common.annotation.Log;
import com.seg.common.core.controller.BaseController;
import com.seg.common.core.domain.AjaxResult;
import com.seg.common.core.page.TableDataInfo;
import com.seg.common.enums.BusinessType;
import com.seg.common.exception.ServiceException;
import com.seg.common.utils.StringUtils;
import com.seg.common.utils.poi.ExcelUtil;
import com.seg.system.domain.TSparePart;
import com.seg.system.domain.TSparePartV;
import com.seg.system.service.ITSparePartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/wms/part")
public class TSparePartController extends BaseController {
    @Autowired
    private ITSparePartService tSparePartService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('wms:part:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSparePart tSparePart) {
        startPage();
        List<TSparePart> list = tSparePartService.selectTSparePartList(tSparePart);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('wms:part:export')")
    @Log(title = "【导出清单】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSparePart tSparePart) {
        List<TSparePart> list = tSparePartService.selectTSparePartList(tSparePart);
        ExcelUtil<TSparePart> util = new ExcelUtil<TSparePart>(TSparePart.class);
        util.exportExcel(response, list, "【导出清单】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:part:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tSparePartService.selectTSparePartById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:part:add')")
    @Log(title = "【新增清单条目】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSparePart tSparePart) {
        int i = tSparePartService.insertTSparePart(tSparePart);
        if (i == -1) {
            return toAjax1(i, "物料号已存在");
        }
        return toAjax(i);
    }

    /**
     * 修改【清单条目 】
     */
    @PreAuthorize("@ss.hasPermi('wms:part:edit')")
    @Log(title = "【清单条目】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSparePart tSparePart) {
        return toAjax(tSparePartService.updateTSparePart(tSparePart));
    }

    /**
     * 删除【删除清单条目】
     */
    @PreAuthorize("@ss.hasPermi('wms:part:remove')")
    @Log(title = "【删除清单条目】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tSparePartService.deleteTSparePartByIds(ids));
    }

    @Log(title = "清单管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wms:part:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TSparePart> util = new ExcelUtil<>(TSparePart.class);
        List<TSparePart> itemList = util.importExcel(file.getInputStream());
        if (StringUtils.isNull(itemList) || itemList.size() == 0) {
            throw new ServiceException("导入清单数据不能为空！");
        }
        String operName = getUsername();
        for (TSparePart tSparePart : itemList) {
            if (tSparePart.getStock() == null) {
                tSparePart.setStock(0L);
            }
            if (StringUtils.isBlank(tSparePart.getLocation())) {
                tSparePart.setLocation("库存");
            }
        }
        String message = tSparePartService.importItemData(itemList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TSparePart> util = new ExcelUtil<>(TSparePart.class);
        util.importTemplateExcel(response, "清单数据");
    }

    @Log(title = "清单管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wms:part:sap:import')")
    @PostMapping("/importSapData")
    public AjaxResult importSapData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TSparePartV> util = new ExcelUtil<>(TSparePartV.class);
        List<TSparePartV> itemList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tSparePartService.importSapData(itemList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importSapTemplate")
    public void importSapTemplate(HttpServletResponse response) {
        ExcelUtil<TSparePartV> util = new ExcelUtil<>(TSparePartV.class);
        util.importTemplateExcel(response, "清单数据");
    }

    @PostMapping("/get_material_type")
    public TableDataInfo getMaterialType() {
        return getDataTable(tSparePartService.getMaterialType());
    }

    @PostMapping("/get_part_status1")
    public TableDataInfo getPartStatus1() {
        return getDataTable(tSparePartService.getPartStatus1());
    }

    @PostMapping("/get_part_status2")
    public TableDataInfo getPartStatus2() {
        return getDataTable(tSparePartService.getPartStatus2());
    }

    @PostMapping("/get_product_line")
    public TableDataInfo getProductLine() {
        return getDataTable(tSparePartService.getProductLine());
    }

    @PostMapping("/get_station")
    public TableDataInfo getStation(@RequestParam(name = "line") String line) {
        return getDataTable(tSparePartService.getStation(line));
    }

    @PostMapping("/get_one_part")
    public AjaxResult getPartById(@RequestParam(name = "id") Long id) {
        TSparePart t = tSparePartService.getPartById(id);
        if (t == null) {
            return error("备件信息不存在");
        }
        return success(t);
    }

    @PostMapping("/get_part_material_num")
    public AjaxResult getPartByMaterialNo(@RequestParam(name = "materialNo") String materialNo) {
        TSparePart t = tSparePartService.getPartByMaterialNo(materialNo);
        if (t == null) {
            return error("备件信息不存在");
        }
        return success(t);
    }

}
